<template>
  <div style="display: flex; flex-direction: column; width: 100%">
    <card-title text="预算信息" />
    <div class="flex-col">
      <div class="flex-row" style="gap: 24px">
        <info-card
          :amount="Number(upperCards.total)"
          title="项目预算"
          pointer
          large
          @click.native="show(0)"
          :selected="currentShow == 0"
        ></info-card>
        <info-card
          :amount="Number(upperCards.thisYear)"
          title="年度预算（本年）"
          pointer
          large
          @click.native="show(1)"
          :selected="currentShow == 1"
        ></info-card>
        <info-card :amount="Number(upperCards.contract)" title="项目合同金额" pointer large></info-card>
      </div>
      <a-card style="margin-top: 12px">
        <div style="gap: 12px; flex-wrap: wrap" class="flex-row">
          <template v-if="currentShow == 0">
            <info-card
              :amount="item.amount"
              :title="item.categoryName"
              v-for="(item, index) in othersList"
              @click.native="showBudgetInfo(item.id)"
              :key="index"
            ></info-card>
            <!--            <info-card :amount='2' title='设备与软件费'></info-card>-->
            <!--            <info-card :amount='2' title='材料/燃料动力费'></info-card>-->
            <!--            <info-card :amount='2' title='外协实验/试验费'></info-card>-->
            <!--            <info-card :amount='2' title='外委研究费'></info-card>-->
            <!--            <info-card :amount='2' title='出版/文献/信息传播/知识产权事务费'></info-card>-->
            <!--            <info-card :amount='2' title='会议/差旅/国际合作与交流费'></info-card>-->
            <!--            <info-card :amount='2' title='专家咨询评审/劳务费'></info-card>-->
            <!--            <info-card :amount='2' title='其他费用'></info-card>-->
          </template>
          <template v-if="currentShow == 1">
            <info-card :amount="Number(item.value)" @click.native="getTotalAmount(item.key)" :title="item.key + '年预算111'" v-for="(item, index) in yearList" :key="index" >
            </info-card>
          </template>
          <a-modal v-model='showYearBudget' :title="year+ '年预算'" width='80%'>
            <a-table :columns='columns' :data-source='tableData' bordered :pagination='false'>
            </a-table>
          </a-modal>
          <a-modal v-model="budgetInfoShow" title="年度预算" width="80%">
            <div v-for="tableInfo in showTable" :key="tableInfo.id">
              <div class="module-title1">
                <p>{{ tableInfo.title }}  </p>
              </div>
              <div class="table-box">
                <a-table
                  :columns="tableInfo.columns"
                  :data-source="getTableData(tableInfo.id)"
                  bordered
                  :pagination="false"
                >
                </a-table>
              </div>
            </div>
          </a-modal>
        </div>
      </a-card>
    </div>
  </div>
</template>

<script>
import CardTitle from '@/views/project/components/CardTitle.vue'
import InfoCard from '@/views/project/components/InfoCard.vue'
import {
  getAllPrice,
  getBudgetInfoByMainId,
  getPrice,
  getSummaryAmount,
  getSummaryYearAmount,
  getDeviceTable,
  getSmallCategory,
  getDataByUrl,
} from '@/api/projectInitApply'

export default {
  name: 'budgetInformation',
  components: { InfoCard, CardTitle },
  data() {
    return {
      columns: [
        {
          title: '项目(课题)经费',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '预算总额',
          dataIndex: 'type',
          scopedSlots: { customRender: 'type' },
        },
      ],
      tableData:[
        {
            key: 3,
            name: '海油发展拨款',
            type: 0,
        },
        {
            key: 4,
            name: '所属单位直接经费配套',
            type: 0,
        },
       
        {
            key: '135987',
            name: '合计',
            type: 0,
        },
    ],
      currentShow: 0,
      yearPriceList:[],
      budgetInfoShow: false,
      upperCards: {
        total: 0,
        thisYear: 0,
        contract: 0,
      },
      yearList: [],
      othersList: [],
      showTable: [{ id: 1 }],
      formContent: [
        {
          id: 1,
          title: '设备购置/试制费（固定资产类）',
          columns: [
            {
              title: '设备名称',
              dataIndex: 'deviceName',
              with: 150,
              value: '',
              scopedSlots: { customRender: 'device' },
            },
            {
              title: '类型',
              dataIndex: 'type',
              value: '',
              scopedSlots: { customRender: 'type' },
            },
            {
              title: '单价（万元/一台）',
              dataIndex: 'price',
              width: 100,
              value: '',
            },
            {
              title: '数量（台套）',
              dataIndex: 'quantity',
              width: 100,
              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              width: 100,
              value: '',
            },
            {
              title: '型号/规格',
              dataIndex: 'spec',
              value: '',
            },
            {
              title: '制造商/国别',
              dataIndex: 'manufacturer',
              value: '',
            },
            {
              title: '主要技术性能指标',
              dataIndex: 'mainTechnical',
              width: 110,
            },
            {
              title: '其他说明（与课题研究任务的关系）',
              dataIndex: 'otherNotes',
              width: 140,
              value: '',
            },
          ],
        },
        {
          id: 2,
          title: '软件费与技术转让费（无形资产类）',
          columns: [
            {
              title: '软件或技术引进名称',
              dataIndex: 'technologyName',

              value: '',
            },
            {
              title: '类型',
              dataIndex: 'type',
              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',

              value: '',
            },
            {
              title: '型号/规格',
              dataIndex: 'spec',

              value: '',
            },
            {
              title: '制造商或服务商、国别',
              dataIndex: 'manufacturer',

              value: '',
            },
            {
              title: '主要技术性能指标',
              dataIndex: 'mainTechnical',
              value: '',
            },
            {
              title: '其他功能与用途等说明（与课题研究任务的关系）',
              dataIndex: 'otherNotes',
              value: '',
            },
          ],
        },
        {
          id: 3,
          title: '材料费',
          columns: [
            {
              title: '材料名称',
              dataIndex: 'materialName',
              value: '',
            },
            {
              title: '单价（万元/计量单位）',
              dataIndex: 'price',
              value: '',
            },
            {
              title: '计量单位',
              dataIndex: 'unit',

              value: '',
            },
            {
              title: '数量',
              dataIndex: 'quantity',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',

              value: '',
            },
            {
              title: '其他说明',
              dataIndex: 'illustrat',
              value: '',
            },

            {
              title: '操作',
              dataIndex: 'action',
              scopedSlots: { customRender: 'action' },

              value: '',
            },
          ],
        },
        {
          id: 4,
          title: '软件费与技术转让费（无形资产费）',
          columns: [
            {
              title: '燃料动力支出项名称',
              dataIndex: 'fuelPowerExpenditureName',
              value: '',
            },
            {
              title: '计量单位',
              dataIndex: 'unit',
              value: '',
            },
            {
              title: '数量',
              dataIndex: 'quantity',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '用途',
              dataIndex: 'useto',
              value: '',
            },
            {
              title: '测算依据说明',
              dataIndex: 'illustrat',
              value: '',
            },

            {
              title: '操作',
              dataIndex: 'action',
              scopedSlots: { customRender: 'action' },
              value: '',
            },
          ],
        },
        {
          id: 5,
          title: '测试化验加工费',
          columns: [
            {
              title: '测试化验加工过服务名称',
              dataIndex: 'testAssayProcessinName',

              value: '',
            },
            {
              title: '具体服务内容及用途说明',
              dataIndex: 'illustrate',
              value: '',
            },
            {
              title: '服务单位',
              dataIndex: 'serviceUnit',

              value: '',
            },
            {
              title: '计量单位',
              dataIndex: 'unit',

              value: '',
            },
            {
              title: '单价（万元/次）',
              dataIndex: 'price',

              value: '',
            },
            {
              title: '数量',
              dataIndex: 'quantity',
              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
          ],
        },
        {
          id: 6,
          title: '试验费',
          columns: [
            {
              title: '试验费支出项构成',
              dataIndex: 'costConstitute',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '科目归口说明及预算合理性依据',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
        {
          id: 7,
          title: '外委研究费',
          columns: [
            {
              title: '外委研究任务名称',
              dataIndex: 'researchTaskName',

              value: '',
            },
            {
              title: '外委主要内容',
              dataIndex: 'mainContent',
              value: '',
            },
            {
              title: '拟外委单位',
              dataIndex: 'unit',

              value: '',
            },
            {
              title: '研究期限',
              dataIndex: 'term',

              value: '',
            },
            {
              title: '预期成果',
              dataIndex: 'yusuan',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
          ],
        },
        {
          id: 8,
          title: '出版/文献/信息传播/知识产权事务费',
          columns: [
            {
              title: '子科目名称',
              dataIndex: 'deviceName',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '测算依据级说明',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
        {
          id: 9,
          title: '科目预算费用组成',
          columns: [
            {
              title: '子科目名称',
              dataIndex: 'subjectName',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '测算依据级说明',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
        {
          id: 10,
          title: '国际合作及交流费',
          columns: [
            {
              title: '对外合作交流名称',
              dataIndex: 'exchangeName',

              value: '',
            },
            {
              title: '交流类型',
              dataIndex: 'exchangeType',
              value: '',
            },
            {
              title: '交流内容',
              dataIndex: 'exchangeContent',

              value: '',
            },
            {
              title: '交流国家/地区',
              dataIndex: 'exchangeCountry',

              value: '',
            },
            {
              title: '出国人数',
              dataIndex: 'abroadPeopleNum',

              value: '',
            },
            {
              title: '出国天数',
              dataIndex: 'abroadDays',
              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
          ],
        },
        {
          id: 11,
          title: '专家咨询评审/劳务',
          columns: [
            {
              title: '子科目名称',
              dataIndex: 'subjectName',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '测算依据及说明',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
        {
          id: 12,
          title: '其他费用',
          columns: [
            {
              title: '子科目名称',
              dataIndex: 'subjectName',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '测算依据及说明',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
        {
          id: 13,
          title: '间接经费配套',
          columns: [
            {
              title: '子科目名称',
              dataIndex: 'subjectName',

              value: '',
            },
            {
              title: '预算额（万元）',
              dataIndex: 'amount',
              value: '',
            },
            {
              title: '测算依据及说明',
              dataIndex: 'illustrate',

              value: '',
            },
          ],
        },
      ],
      showYearBudget:false,
      formData: [],
      year:null,
    }
  },
  mounted() {
    this.budgetInfo()
    this.allPrice()
    this.initSmallCategory()
    this.yearAllPriceList()
  },
  methods: {
    getTotalAmount(year){
      this.year = year
      this.showYearBudget = true;
      let data = this.yearPriceList;
      console.log(this.yearPriceList);
      if(year==data[0].years[0]){
        data[0].list.forEach(ele=>{
          this.tableData.forEach(table=>{
            console.log(table,ele);
            if(table.key== ele.payerId){
              table.type+=ele.amount
            }
          })
        })
      }
      this.tableData.forEach(ele=>{
        this.tableData.forEach(ele2=>{
          if(ele.key==135987){
            if(ele2.key!=135987){
              ele.type+=ele2.type
            }
          }
        })
      })

    },
    show(index) {
      this.currentShow = index
    },
    budgetInfo() {
      getSummaryAmount({ projectApplicationId: this.$route.query.id }).then((res) => {
        this.upperCards = res.data
      })
      getSummaryYearAmount({ projectApplicationId: this.$route.query.id }).then((res) => {
        this.yearList = res.data
      })
      // getBudgetInfoByMainId(this.$route.query.id).then(res => {
      //   this.upperCards = res.data.map(v => Number(v))
      // })
    },
    allPrice() {
      // getAllPrice({ mainId: this.$route.query.id }).then(res => {
      //   console.log('子类数据：')
      //   console.log(res.data)
      // })
      getPrice({ projectApplicationId: this.$route.query.id }).then((res) => {
        this.othersList = res.data
      })
    },
    yearAllPriceList(){
      getAllPrice({
        mainId: this.$route.query.id
      }).then(res=>{
        this.yearPriceList = res.data
      })
    },
    initSmallCategory() {
      getSmallCategory().then((res) => {
        this.smallCategoryList = res.data
      })
    },
    showBudgetInfo(id) {
      const categories = this.smallCategoryList.filter((ele) => ele.categoryId == id)
      this.showTable = []
      this.formData = []
      categories.forEach((category) => {
        const beanName = category.bean
        let url = beanName.substring(5)
        url =
          '/pm-center/fna/' +
          url.substring(0, 1).toLowerCase() +
          url.substring(1) +
          '/getByProjectApplicationId?projectApplicationId=' +
          this.$route.query.id
        this.showTable.push(this.formContent[category.id - 1])
        getDataByUrl(url).then((res) => {
          this.formData.push({id:category.id, data: res.data})
        })
      })
      this.budgetInfoShow = true
    },
    getTableData(id){
      if(this.formData.length == 0){
        return [];
      }else{
        let element = this.formData.find(ele=>ele.id == id);
        if(element) {
          return element.data;
        }else{
          return [];
        }
      }
    },

  },
}
</script>

<style scoped lang='less'>
.flex-row {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.flex-col {
  display: flex;
  flex-direction: column;
}
</style>
